package com.zou.szt;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wandoujia.ads.sdk.Ads;
import com.zou.szt.Utils.GetCards;
import com.zou.szt.Utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements BaseDialogFragment.OnSubmitListener, BaseRecyclerViewAdapter.OnRequestOverListener {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<String> data;
    private MyAdapter myAdapter;

    private static final String APP_ID = "100040116";
    private static final String SECRET_KEY = "de67a1f5110f78786accdc986a1145fe";
    private static final String BANNER = "dc8ccc95488c4d46f713f4465f61803b";
    private LinearLayout ad_container;

    //    监听fragment添加和删除事件
    @Override
    public void sendMessage(String message) {
        data.clear();
        if (GetCards.getData(this) != null) {
            data.addAll(GetCards.getData(this));
        }
        myAdapter.notifyDataSetChanged();
    }

    //    监听RecyclerView数据刷新事件
    @Override
    public void requestOver() {
        swipeRefreshLayout.setRefreshing(false);
    }


    //    监听下拉刷新事件接口
    public interface OnSwipeRefreshListener {
        void getData();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        Button warnbtn = (Button) toolbar.findViewById(R.id.iconwarn2);
        warnbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment dialogFragment = new DialogFragment() {
                    @NonNull
                    @Override
                    public Dialog onCreateDialog(Bundle savedInstanceState) {
                        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_warn, null);

                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setView(view).setTitle("注意！");
                        builder.setPositiveButton("确认", null);

                        return builder.create();

                    }
                };

                dialogFragment.show(getSupportFragmentManager(), "warn");
            }
        });
/*//显示自定义actionbar开关
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(R.layout.layout_actionbar);
        Button warnbtn = (Button) actionBar.getCustomView().findViewById(R.id.right_bt);
        warnbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment dialogFragment = new DialogFragment() {
                    @NonNull
                    @Override
                    public Dialog onCreateDialog(Bundle savedInstanceState) {
                        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_warn, null);

                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setView(view).setTitle("注意！");
                        builder.setPositiveButton("确认", null);

                        return builder.create();

                    }
                };

                dialogFragment.show(getSupportFragmentManager(), "warn");
            }
        });*/

        initView();


        ad_container = (LinearLayout) findViewById(R.id.ad_container);
        new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... params) {
                try {
                    Ads.init(MainActivity.this, APP_ID, SECRET_KEY);
                    return true;
                } catch (Exception e) {
                    Log.e("ads-sample", "error", e);
                    return false;
                }
            }

            @Override
            protected void onPostExecute(Boolean success) {

                if (success) {
                    /**
                     * pre load
                     */
                    Ads.preLoad(BANNER, Ads.AdFormat.banner);

                    /**
                     * add ad views
                     */
                    View bannerView = Ads.createBannerView(MainActivity.this, BANNER);
                    ad_container.addView(bannerView);
                }
            }
        }.execute();


    }

    private void initView() {

//        RecyclerView
        recyclerView = (RecyclerView) findViewById(R.id.rcv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        data = new ArrayList<>();
        if (GetCards.getData(this) != null) {
            data.addAll(GetCards.getData(this));
        }
        myAdapter = new MyAdapter(data, this, getSupportFragmentManager());
        recyclerView.setAdapter(myAdapter);


//        下拉刷新控件
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setColorSchemeColors(Color.parseColor("#F44336"));
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (data.size() > 0) {
                    myAdapter.getData();
                } else {
                    swipeRefreshLayout.setRefreshing(false);
                    ToastUtil.showToast(MainActivity.this, "请先添加深圳通卡号");
                }
            }
        });

    }

}
